// pages/myOrder/myOrder.js
const app = getApp()
var util =  require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor: app.globalData.themeColor,
    isHistory: false,
    isOrder: false,
    date: '',
    userId: '',
    orderList: [],
    historyList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
   onLoad: function (options) {
    var that = this
    that.setData({
      date: util.setTime(new Date())
    })
  },
  getData(){
    var that = this
    util.getAjax(app.globalData.apiUrl +'/Api/Food/get_user_caipu',function(res){
      console.log(res)
      if(res.data.list.length != 0){
        that.setData({
          isOrder: true,
          orderList: res.data.list
        })
      }
    },{user_id: that.data.userId,date: that.data.date})
  },
  //获取历史数据
  getList:function(){
    var that = this
    util.getAjax(app.globalData.apiUrl +'/Api/Food/get_user_caipu',function(res){
      console.log(res)
      if(res.data.length != 0){
        for(var i in res.data){
          res.data[i].date = res.data[i].date.replace(/\//g,'-')
          res.data[i].week = that.getWeek(new Date(res.data[i].date).getDay())
          res.data[i].isShowDetails = false
        }
        that.setData({
          isHistory: true,
          historyList: res.data
        })
      }
    },{user_id: that.data.userId})
  },

  //获取星期
  getWeek:function(type){
    var text = ''
    switch (type) {
      case 0:
        text = '周日'
        break;
      case 1:
        text = '周一'
        break;
     case 2:
        text = '周二'
        break;
      case 3:
        text = '周三'
        break;
      case 4:
        text = '周四'
        break;
      case 5:
        text = '周五'
        break;
      case 6:
        text = '周六'
        break;
    }
    return text
  },
  detailsChange(e){
    const {
      index
    } = e.currentTarget.dataset;
    var that = this
    let list = that.data.historyList
    list[index].isShowDetails = !list[index].isShowDetails
    that.setData({
      historyList: list,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  /**
   * 生命周期函数--监听页面显示
   */
   onShow: function () {
    var that = this
    
    if(app.globalData.userId){
      that.setData({
        userId: app.globalData.userId
      })
      that.getList();
      that.getData();
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          that.setData({
            userId: userId
          });
          that.getList();
          that.getData();
        }
      }
    }
    if(app.globalData.contentId){
      that.setData({
        contentId: app.globalData.contentId
      })
    }else{
      app.contentIdCallback = contentId => {
        if (contentId != '') {
          that.setData({
            contentId: contentId
          });
        }
      }
    }
    if(app.globalData.status){
      that.setData({
        status: app.globalData.status
      })
    }else{
      app.statusCallback = status => {
        if (status != '') {
          that.setData({
            status: status
          });
        }
      }
    }
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})